<template>
  <el-card class="box-card">
    <el-form :model="loginUser" :rules="rules" ref="loginUser" label-width="100px" class="demo-ruleForm">
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="loginUser.phone"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="loginUser.password" show-password></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login('loginUser')">登录</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
export default {
  name: "LoginView",
  data() {
    return{
      loginUser:{},
      rules: {
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods:{
    login(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.axios.post(this.$monthHost + 'user/login',this.loginUser).then(res=>{
            if (res.data.result) {
              this.$message.success('登录成功');
              window.localStorage.setItem('token',res.data.data);
              this.$router.push('/list');
            } else {
              this.$message.error(res.data.msg);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  }
}
</script>

<style scoped>
.box-card {
  margin-left: 20%;
  margin-right: 20%;
}
</style>